let fun = {};
fun.getBirthdayFromIdCard = function (idCard) {
  let birthday = '';
  if(idCard !== null && idCard !== "" && idCard.length === 18){  
    birthday = idCard.substr(6,8).replace(/(.{4})(.{2})/,"$1-$2-");
  }
  return birthday;
};

fun.getAgeByBirthday = function (birthday) {
  let age;
  if (/^(\d{4})-(\d{2})-(\d{2})$/.test(birthday)) {
    let fullYear = birthday.split('-');
    let date = new Date();
    date.setFullYear(parseInt(fullYear[0]),parseInt(fullYear[1]),parseInt(fullYear[2]));
    let timeBirthD = Date.parse(date);
    let timeNow = Date.parse(new Date());
    age = parseInt((timeNow - timeBirthD) / (1000 * 3600 * 24 * 365));
  }
  return age;
};

fun.chackIdCard = function (idCard) {
  return (idCard && /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(idCard));
};

export default fun;
